/**
 *  @author Ion Morozan
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.dddforandroid.design;

import java.util.ArrayList;
import java.util.List;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.dddforandroid.database.R;

public class AccountList extends Activity {

	/**
	 * The AppEngine app name, used to construct the production service URL
	 * below.
	 */
	private static final String APP_NAME = "3dforandroid";

	/**
	 * The URL of the production service.
	 */
	public static final String PROD_URL = "https://" + APP_NAME
			+ ".appspot.com";

	/**
	 * The selected position in the ListView of accounts.
	 */
	private int mAccountSelectedPosition = 0;

	/**
	 * current context.
	 */
	private Context currentContext = this;

	protected AccountManager accountManager;

	public static final String DEVICE_REGISTRATION_ID = "deviceRegistrationID";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle(" Active Accounts ");

		setContentView(R.layout.connect);
		setConnectScreenContent();

	}

	/**
	 * Sets up the 'connect' screen content.
	 */
	private void setConnectScreenContent() {
		List<String> accounts = getGoogleAccounts();
		if (accounts.size() == 0) {
			// Show a dialog and invoke the "Add Account" activity if requested
			AlertDialog.Builder builder = new AlertDialog.Builder(
					currentContext);
			builder.setMessage(R.string.needs_account);
			builder.setPositiveButton(R.string.add_account,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							startActivity(new Intent(
									Settings.ACTION_ADD_ACCOUNT));
						}
					});
			builder.setNegativeButton(R.string.skip,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							finish();
						}
					});
			builder.setIcon(android.R.drawable.stat_sys_warning);
			builder.setTitle(R.string.attention);
			builder.show();
		} else {

			final ListView listView = (ListView) findViewById(R.id.select_account);

			listView.setAdapter(new ArrayAdapter<String>(currentContext,
					R.layout.account, accounts));

			listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
			listView.setItemChecked(mAccountSelectedPosition, true);

			final Button connectButton = (Button) findViewById(R.id.connect);
			connectButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {

					mAccountSelectedPosition = listView
							.getCheckedItemPosition();
					TextView account = (TextView) listView
							.getChildAt(mAccountSelectedPosition);

					Account selectedaccount = new Account(account.getText()
							.toString(), "com.google");
					Intent intentTasks = new Intent(currentContext,
							CreateTabsActivity.class);
					intentTasks.putExtra("account", selectedaccount);
					startActivity(intentTasks);
					finish(); 
				}
			});
		}
	}

	/**
	 * Returns a list of registered Google account names. If no Google accounts
	 * are registered on the device, a zero-length list is returned.
	 */
	private List<String> getGoogleAccounts() {
		ArrayList<String> result = new ArrayList<String>();
		Account[] accounts = AccountManager.get(currentContext).getAccounts();
		for (Account account : accounts) {
			if (account.type.equals("com.google")) {
				result.add(account.name);
			}
		}
		return result;
	}

}
